<template>
  <div>
    <el-dialog v-model="props.inheritAttrs"  @close="close" style="width:400px;height: 260px;margin-top: 100px;" :title="showId?'修改目录':'新增目录'">
      <el-form :model="formData" ref="formsRef" :rules="rules" label-position="left" label-width="80px" size="medium"
    >
    <el-form-item label="所属学科" prop="subjectID">
      <el-select v-model="formData.subjectID" class="full-width-input" style="width:280px">
        <el-option v-for="(item, index) in dataList" :key="index" :label="item.label"
          :value="item.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="目录名称" prop="tagName">
      <el-input v-model="formData.tagName" type="text"></el-input>
    </el-form-item>
    <el-form-item style="float: right;">
          <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
        </el-form-item>
  </el-form>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, reactive, watch, onMounted, getCurrentInstance, nextTick } from 'vue'
import { simple } from '@/api/hmmm/subjects'
import { add, update } from '@/api/hmmm/tags'
import { ElMessage, ElMessageBox } from 'element-plus';

const props = defineProps(['inheritAttrs','showId'])
const emits = defineEmits(['update:dialogVisible','getList'])//关闭窗口
let dataList = ref([])
let formData = ref({
    subjectID: undefined,
    tagName: undefined,
    showId:undefined
  })
let formsRef=ref()
defineExpose({formData})

onMounted(()=>{
  getSubject()
})
const getSubject = async()=>{
   const res = await simple()
   dataList.value = res.data
}
const rules =ref({
  subjectID: [{
    required: true,
    message: '请选择所属学科',
    trigger: 'blur'
  }],
  tagName: [{
    required: true,
    message: '请输入目录名称',
    trigger: 'blur'
  }],
})
    
const  close =()=> {
  emits('update:dialogVisible')
}
const instance = getCurrentInstance()
const submitForm = () => {
  instance.ctx.$refs['formsRef'].validate(async valid => {
    if(formData.value.showId){
      await update({subjectID:formData.value.subjectID,tagName:formData.value.tagName,id:formData.value.showId})
      ElMessage.success('修改成功')
    }else{
      await add({subjectID:formData.value.subjectID,tagName:formData.value.tagName})
      ElMessage.success('添加成功')
    }
    emits('getList')
    close()
  })
}
const resetForm = () => {
  instance.ctx.$refs['formsRef'].resetFields()
  close()
}
</script>
<style>
</style>

